package com.opensms.mt.ms;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Calendar;

import com.opensms.eventLogFacade.EventLogFacade;
import com.opensms.eventLogFacade.vo.EventLogVO;
import com.opensms.mt.MTFacade;
import com.util.exceptions.InternalErrorException;


/**
 * Enviar sms utilizando el API de Movistart
 * Utiliza EnviarAction
 *
 */
public class MSMTFacade implements MTFacade {
	
	private boolean logActive = false;
	
	public boolean isLogActive(){
		return logActive;
	}
	
	public void setLogActive(boolean logActive){
		this.logActive = logActive;
	}
	
	public String enviarSMS(String tomovil, String msg) 
		throws InternalErrorException{

		EventLogFacade fac = new EventLogFacade();
		try {
			EnviarAction action = new EnviarAction();
			String rdo = action.enviar(tomovil,msg);
			if (logActive){
				fac.createEventLog(new EventLogVO(0, 
					new Timestamp(Calendar.getInstance().getTimeInMillis()), EventLogVO.ENVIO,
					EventLogVO.SYSTEM, tomovil, msg, rdo));
			}
			return rdo;
		} catch (IOException e) {
			if (logActive){
				fac.createEventLog(new EventLogVO(0, 
					new Timestamp(Calendar.getInstance().getTimeInMillis()), EventLogVO.ENVIO,
					EventLogVO.SYSTEM, tomovil, msg, e.getMessage()));
			}
			throw new InternalErrorException(e);
		}
	}

}
